package com.lantosec.hmrp.modules.cms.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.cms.entity.CmsCategory;
import com.lantosec.hmrp.modules.cms.service.ICmsCategoryService;
import com.lantosec.hmrp.modules.cms.vo.CmsCategoryVO;

import java.util.Arrays;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

/**
 *
 * CMS栏目管理 控制器
 *
 * @author AutoCode
 * @since 2020-07-06
 */
@Api(value = "CMS栏目管理控制器", tags = "CMS栏目管理控制器")
@RestController
@RequestMapping("/cms/category")
@Log4j2
public class CmsCategoryController extends BaseController {
    @Autowired
    private ICmsCategoryService service;

    @GetMapping("getCmsCategory")
    @ApiOperation(value = "查询全部CMS栏目树")
    public Result<List<CmsCategoryVO>> getCmsCategory(@ApiParam(value = "categoryId") @RequestParam(name="categoryId",required = false) String categoryId){
        List<CmsCategoryVO> cmsCategoryVOS = service.getCmsCategory(categoryId);
        return Result.ok(cmsCategoryVOS);
    }

    @GetMapping("all")
    @ApiOperation(value = "查询全部CMS栏目")
    public Result<List<CmsCategoryVO>> all(@ApiParam(value = "categoryId") @RequestParam(name="categoryId",required = false) String categoryId){
        List<CmsCategoryVO> cmsCategoryVOS = service.getAll(categoryId);
        return Result.ok(cmsCategoryVOS);
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<Page<CmsCategoryVO>> page(@ApiParam(value = "分页对象", required = true) Page<CmsCategory> page, CmsCategoryVO condition){
        page = service.page(page,
                new QueryWrapper<CmsCategory>()
//                        .like(StringUtils.isNotEmpty(condition.getUsername()),"username",condition.getUsername())
        );
        return Result.ok(entity2VO(page));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<CmsCategoryVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ApiOperation(value = "新增CMS栏目")
    public Result<String> create(@ApiParam(value = "CMS栏目管理VO对象", required = true) @RequestBody @Validated CmsCategoryVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        CmsCategory entity = vo.convertToEntity();
        //查询最大的id
        String categoryId = service.queryMaxId(vo.getParentId());
        entity.setCategoryId(categoryId);
        service.save(entity);
        return Result.ok(entity.getCategoryId());
    }

    @PutMapping("update")
    @ApiOperation(value = "修改CMS栏目")
    public Result<Void> update(@ApiParam(value = "CMS栏目管理VO对象", required = true) @RequestBody CmsCategoryVO vo){
        CmsCategory entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除CMS栏目")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }
}
